#include <buola/chart/plots/cbetweenplot.h>

#include <buola/image/cgc.h>
#include <buola/chart/cxychart.h>
#include <buola/chart/cxaxis.h>
#include <buola/chart/cyaxis.h>

namespace buola { namespace chart {

CBetweenPlot::CBetweenPlot()
{
    mColor=img::CColor(1,0,0);
    mLineWidth=2;
    mPointDiameter=5;
    mStyle=CPlot::LINE;
}

CBetweenPlot::~CBetweenPlot()
{
}

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
////////////////////////// Interface //////////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

void CBetweenPlot::SetSeries1(PSeries pSeries)
{
    mSeries1=pSeries;
}

void CBetweenPlot::SetSeries2(PSeries pSeries)
{
    mSeries2=pSeries;
}

void CBetweenPlot::SetColor(const img::CColor &pColor)
{
    mColor=pColor;
}

void CBetweenPlot::SetLineWidth(double pWidth)
{
    mLineWidth=pWidth;
}

void CBetweenPlot::SetPointDiameter(double pDiameter)
{
    mPointDiameter=pDiameter;
}

void CBetweenPlot::Draw(CXYChartContext &pC)
{
    if(!mSeries1->size()) return;

    CPoint_d lFirst(pC.mXAxis->Map(mSeries1->GetX(0)),
                  pC.mYAxis->Map(mSeries1->GetY(0)));
    pC.mGC->Path().MoveTo(CPoint_d(lFirst));
    CPoint_d lLast=lFirst;

    for(int j=1;j<mSeries1->size();j++)
    {
        CPoint_d lNext(pC.mXAxis->Map(mSeries1->GetX(j)),
                     pC.mYAxis->Map(mSeries1->GetY(j)));
        if(lNext.x-lLast.x<2&&j<mSeries1->size()-1) continue;
        pC.mGC->Path().LineTo(lNext);
        lLast=lNext;
    }

    lFirst={pC.mXAxis->Map(mSeries2->GetX(mSeries2->size()-1)),pC.mYAxis->Map(mSeries2->GetY(mSeries2->size()-1))};
    pC.mGC->Path().LineTo(lFirst);
    lLast=lFirst;

    for(int j=mSeries2->size()-2;j>=0;j--)
    {
        CPoint_d lNext(pC.mXAxis->Map(mSeries2->GetX(j)),
                     pC.mYAxis->Map(mSeries2->GetY(j)));
        if(lLast.x-lNext.x<2&&j>0) continue;
        pC.mGC->Path().LineTo(lNext);
        lLast=lNext;
    
    }

    pC.mGC->Path().Close();
    img::CColor lColor=mColor;
    lColor.a=0.3;
    pC.mGC->SetSource(lColor);
    pC.mGC->FillPath();
}

/*namespace chart*/ } /*namespace buola*/ }
